package xcmg.device.web.account;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.account.CheckService;
import xcmg.device.service.vo.account.CheckAddVO;
import xcmg.device.service.vo.account.CheckVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;

@RestController
@RequestMapping("/check")
public class CheckController extends BasicController {

    @Autowired
    private CheckService checkService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult warehouse(@RequestBody CheckAddVO checkAddVO)throws Exception{
        if(null == checkAddVO ||
                null == checkAddVO.getDetailList() ||
                checkAddVO.getDetailList().isEmpty()){
            return actionResult(ErrorCode.IllegalArument);
        }

        checkAddVO.getHeaderInfo().setCreateId(getUserId());
        checkAddVO.getHeaderInfo().setCreateName(getUserName());
        try {
            return actionResult(checkService.add(checkAddVO,getOrgId(),getUserId()));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/reverse",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult warehouse(@RequestBody CheckVO checkVO)throws Exception{
        if(null == checkVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        try {
            return actionResult(checkService.reverse(checkVO.getId(),getOrgId(),getUserId()));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id){
        return actionValueResult(checkService.item(id));
    }

    //审核通过
    @RequestMapping(value = "/audit",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult audit(String id){
        return actionResult(checkService.audit(id,true));
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<CheckVO> condition)throws Exception{
        CheckVO checkVO = condition.getFilters();
        if(checkVO == null){
            checkVO = new CheckVO();
        }
        if(checkVO.getCheckType() == 0){
            checkVO.setSupplierOrgId(getOrgId());
        }else{
            checkVO.setPurchaseOrgId(getOrgId());
        }
        condition.setFilters(checkVO);
        PageCommonVO pageCommonVO = checkService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/export",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void export( String[] ids)throws Exception {
        checkService.export(Arrays.asList(ids));
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void exportAll(CheckVO condition)throws Exception {
        if(condition.getCheckType() == 0){
            condition.setSupplierOrgId(getOrgId());
        }else{
            condition.setPurchaseOrgId(getOrgId());
        }
        checkService.exportAll(condition);
    }
}
